#include "head.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define BUFFERSIZE 4096

int main(int argc,char *argv[])
{
    int fd; 
    //printf("%d", argc);
    if(argc != 3){
    	printf("Error!\n<head/tail [FILE]...>\n");
    	exit(1);
    }
    if((fd =open(argv[2],O_RDONLY,0)) == -1 ){
                error("Cannot open ", argv[2]);
                exit(1);
    }
    char ch[BUFFERSIZE];
    int size = read(fd,&ch,BUFFERSIZE);
    if(size == -1)
    	error("Read error from ", argv[2]);
    //printf("%d\n", size);
    if (close(fd) == -1)
        error("Error closing files", argv[2]);
    if(strcmp(argv[1], "head")==0){
        newHead(ch,size);
    }
    else if(strcmp(argv[1], "tail")==0){
        newTail(ch,size);
    }
    else{
    	printf("Error!\n<head/tail [FILE]...>\n");
    }
    return 0;
}
